package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.Checkwork;
import com.example.wechatdaka.entity.Signin;
import com.example.wechatdaka.entity.SigninTotal;
import com.example.wechatdaka.service.CheckworkService;
import com.example.wechatdaka.service.SigninService;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-30
 */
@Api(tags = "签到模块")
@RestController
@RequestMapping("/signin")
public class SigninController {
    @Autowired
    private SigninService signinService;
    @Autowired
    private CheckworkService checkworkService;
    @ApiOperation(value = "微信签到")
    @RequestMapping(value = "/saveSignin",method = RequestMethod.POST)
    public Result saveSignin(String lxCheckWorkId,String lxUserNumber){
        Checkwork checkwork = checkworkService.getById(lxCheckWorkId);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowTime=new Date();
        if (checkwork!=null){
            QueryWrapper<Signin> signinQueryWrapper=new QueryWrapper<>();
            signinQueryWrapper.eq("lxCheckworkId",lxCheckWorkId).eq("lxUserNumber",lxUserNumber);
            Signin one = signinService.getOne(signinQueryWrapper);
            if (one!=null){
                return new Result(Result.ERROR,null,"重复签到");
            }
            long nowTimeMillisecond=nowTime.getTime();  //当前时间
            long endTimeMillisecond=checkwork.getLxReleaseTime().getTime()+Integer.parseInt(checkwork.getLxCheckAging())*60*1000;   //签到发布时间+有效时间
            if(nowTimeMillisecond>endTimeMillisecond){
                return new Result(Result.ERROR,null,"超出考勤时间范围");
            }else {
                Signin signin=new Signin();
                signin.setLxCheckworkId(lxCheckWorkId);
                signin.setLxUserNumber(lxUserNumber);
                signin.setLxSignInStatus(1);
                signin.setLxSignInTime(nowTime);
                boolean save = signinService.save(signin);
                if (save){
                    return new Result(Result.SUCCESS,null,"SUCCESS");
                }else {
                    return new Result(Result.ERROR,null,"签到失败");
                }
            }
        }
        return new Result(Result.ERROR,null,"无此考勤信息");
    }
    @ApiOperation(value = "微信查看签到历史")
    @RequestMapping(value = "/querySigninMini",method = RequestMethod.POST)
    public Result querySigninMini(String lxUserNumber){
        List<Signin> signinList = signinService.querySigninMini(lxUserNumber);
        return new Result(Result.SUCCESS,signinList,"SUCCESS");
    }
    @ApiOperation(value = "分页根据考勤id查看签到结果")
    @RequestMapping(value = "/querySignByCheckPage",method = RequestMethod.GET)
    public JsonResult querySignByCheckPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize, String id) {
        Page<Signin> page=new Page<>(currentPage,pagesize);
        IPage<Signin> signinIPage=signinService.querySignByCheckPage(page,id);
        List<Signin> records = signinIPage.getRecords();
        long total = signinIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页根据考勤id和关键字查看签到结果")
    @RequestMapping(value = "/querySignByCheckAndKeyword",method = RequestMethod.GET)
    public JsonResult querySignByCheckAndKeyword(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                           @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize, String id,String keyword) {
        Page<Signin> page=new Page<>(currentPage,pagesize);
        IPage<Signin> signinIPage=signinService.querySignByCheckAndKeyword(page,id,keyword);
        List<Signin> records = signinIPage.getRecords();
        long total = signinIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页查看所有考勤签到结果")
    @RequestMapping(value = "/queryAllSignPage",method = RequestMethod.GET)
    public JsonResult queryAllSignPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                           @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String lxUserId) {
        Page<Signin> page=new Page<>(currentPage,pagesize);
        IPage<Signin> signinIPage=new Page<>();
        if ("1".equals(lxUserId)){
            signinIPage=signinService.queryAllSignPage(page);
        }else {
            signinIPage=signinService.querySignByUserIdPage(page,lxUserId);
        }
        List<Signin> records = signinIPage.getRecords();
        long total = signinIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "更新签到状态")
    @RequestMapping(value = "/updateSignStatus",method = RequestMethod.GET)
    public Result updateSignStatus(String id,Integer lxSignInStatus){
        Signin signin = signinService.getById(id);
        if (signin==null){
            return new Result(Result.ERROR,null,"ERROR");
        }
        Date date=new Date();
        signin.setLxSignInTime(date);
        signin.setLxSignInStatus(lxSignInStatus);
        boolean update = signinService.updateById(signin);
        if (update){
            return new Result(Result.SUCCESS,null,"SUCCESS");
        }
        return new Result(Result.ERROR,null,"ERROR");
    }
    @ApiOperation(value = "分页查看所有考勤结果统计")
    @RequestMapping(value = "/queryAllSignStatisticsPage",method = RequestMethod.GET)
    public JsonResult queryAllSignStatisticsPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                       @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String lxUserId) {
        Page<SigninTotal> page=new Page<>(currentPage,pagesize);
        IPage<SigninTotal> signinIPage=new Page<>();
        if ("1".equals(lxUserId)){
            signinIPage=signinService.queryAllSignStatistics(page);
        }else {
            signinIPage=signinService.querySignStatisticsByUserIdPage(page,lxUserId);
        }
        List<SigninTotal> records = signinIPage.getRecords();
        long total = signinIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页根据关键字查看考勤结果统计")
    @RequestMapping(value = "/querySignKeywordPage",method = RequestMethod.GET)
    public JsonResult querySignStatisticsKeywordPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                                 @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String keyword) {
        Page<SigninTotal> page=new Page<>(currentPage,pagesize);
        IPage<SigninTotal> signinIPage=signinService.querySignStatisticsByKeywordPage(page,keyword);
        List<SigninTotal> records = signinIPage.getRecords();
        long total = signinIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }

}

